import { ElLoading } from "element-plus";

let loadingCount = 0;

const loadingOptions = {
  lock: true,
  text: "请求处理中，请稍后...",
  spinner: "el-icon-loading",
  background: "rgba(255, 255, 255, 0.8)"
};

let loadingInstance: { close: () => void };

const startLoading = () => {
  loadingInstance = ElLoading.service(loadingOptions);
};

// when a request finished
const endLoading = () => {
  loadingInstance.close();
};

// when many request start
export const showLoading = () => {
  if (loadingCount == 0) {
    startLoading();
  }
  loadingCount += 1;
};

export const hideLoading = () => {
  if (loadingCount <= 0) {
    return;
  }
  loadingCount -= 1;
  if (loadingCount === 0) {
    endLoading();
  }
};
